﻿using BCCommon;
using BCData;
using BCEntity.Common.Payment.PaymentCallbackRecord;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace ATSimData.PaymentCallbackRecord
{
    /// <summary>
    /// 支付回调记录数据处理
    /// </summary>
    public class PaymentCallbackRecordData : IPaymentCallbackRecordData
    {
        private const string QUERY = @"SELECT PaymentCallbackRecordId,PayChannelType,UserId,DataParameters,
                                        RemakeContent,CreateTime,AdminId FROM PaymentCallbackRecord";
        protected readonly IDatabaseContext databaseContext;
        public PaymentCallbackRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        /// <summary>
        /// 添加支付回调记录
        /// </summary>
        /// <param name="entity">支付回调记录实体</param>
        /// <returns></returns>
        public long Add(PaymentCallbackRecordEntity entity)
        {
            string sql = @"INSERT INTO PaymentCallbackRecord(PayChannelType,UserId,AdminId,DataParameters)VALUES(?PayChannelType,?UserId,?AdminId,?DataParameters);
                            SELECT LAST_INSERT_ID();";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PayChannelType", entity.PayChannelType.ToString(), DbType.AnsiString);
            parameters.Add("?UserId", entity.UserId, DbType.Int64);
            parameters.Add("?AdminId", entity.AdminId, DbType.Int64);
            parameters.Add("?DataParameters", entity.DataParamters, DbType.AnsiString, size: 2000);
            return this.databaseContext.Master.ExecuteScalar<long>(sql, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 获取支付回调记录集合
        /// </summary>
        /// <param name="index">起始页码</param>
        /// <param name="size">页面大小</param>
        /// <returns></returns>
        public async Task<(IEnumerable<PaymentCallbackRecordEntity>,int)> GetPaymentCallbackRecords(int pageIndex, int pageSize)
        {
            string sql = string.Format(@"{0} ORDER BY PaymentCallbackRecordId DESC LIMIT {1},{2}; SELECT (1) FROM PaymentCallbackRecord", QUERY,
                 (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize);
            return await this.databaseContext.Slave.QueryMultipleAsync<PaymentCallbackRecordEntity, int>(sql);
        }
        /// <summary>
        /// 更新备注内容
        /// </summary>
        /// <param name="paymentCallbackRecordId">支付回调记录Id</param>
        /// <param name="RemakeContent">备注内容</param>
        /// <returns></returns>
        public bool UpdateRemakeContent(long paymentCallbackRecordId, string remakeContent)
        {
            string sql = @"UPDATE PaymentCallbackRecord SET 
                                IsComplete = 1,
                                RemakeContent = ?RemakeContent
                           WHERE PaymentCallbackRecordId=?PaymentCallbackRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PaymentCallbackRecordId", paymentCallbackRecordId, DbType.Int64);
            parameters.Add("?RemakeContent", remakeContent, DbType.AnsiString, size: 500);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }

        /// <summary>
        /// 更新支付回调记录状态
        /// </summary>
        /// <param name="paymentCallbackRecordId">支付回调记录Id</param>
        /// <returns></returns>
        public bool UpdateStatus(long paymentCallbackRecordId)
        {
            string sql = @"UPDATE PaymentCallbackRecord SET
                                IsComplete = 1
                           WHERE PaymentCallbackRecordId=?PaymentCallbackRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PaymentCallbackRecordId", paymentCallbackRecordId, DbType.Int64);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }
    }
}
